<div class="approved-wrap p-4" style="display: none">
  <div class="d-flex align-items-center mb-4">
    <div class="me-2">{{ __('MultiSeller::common.text_approved') }}</div>
    <select name="busy_code" class="form-select w-auto">
      <option value="1">{{ __('MultiSeller::common.text_pass') }}</option>
      <option value="0">{{ __('MultiSeller::common.text_reject') }}</option>
    </select>
  </div>
  <div class="d-flex">
    <div class="me-2 text-nowrap">{{ __('MultiSeller::common.text_remark') }}</div>
    <textarea class="form-control remark-input" rows="3"></textarea>
  </div>
</div>

@push('footer')
<script>
  $(document).on('click', '.btn-approved', function () {
    let $btn = $(this);
    let id = $(this).parent('td').siblings('td').eq(0).find('input').val();

    layer.open({
      type: 1,
      title: '{{ __('MultiSeller::common.text_approved') }}',
      area: ['400px', '300px'],
      content: $('.approved-wrap'),
      btn: ['{{ __('common.cancel') }}', '{{ __('common.confirm') }}'],
      yes: function (index, layero) {
        layer.close(index);
      },
      btn2: function (index, layero) {
        const data = {
          status: layero.find('select[name="busy_code"]').val(),
          comment: layero.find('.remark-input').val()
        }

        $http.post(`products/${id}/approve`, data).then((res) => {
          layer.msg(res.message);
          $btn.hide();
          layer.close(index);
        })

        return false;
      },

      end: function () {
        $('.remark-input').val('');
      }
    });
  });

  $(document).ready(function () {
    const ids = [];

    $('.btn-approved').each(function () {
      ids.push($(this).parent('td').siblings('td').eq(0).find('input').val());
    });

    // 判断哪些商品需要审核
    $http.post('products/need_approve', {ids}, {hload: true}).then((res) => {
      $('.btn-approved').each(function () {
        let id = $(this).parent('td').siblings('td').eq(0).find('input').val();
        if (res.data.need_approve[id]) {
          $(this).removeClass('d-none')
        }
      });
    })
  });

</script>
@endpush
